package com.atcx.service.impl;

import com.atcx.mapper.UserMapper;
import com.atcx.pojo.SysUser;
import com.atcx.pojo.SysUserExample;
import com.atcx.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Copyright (C), 2017-2022, MIDOU
 * ClassName: UserServiceImpl
 * Author:   pangshu
 * Date:     2022/7/21 14:34
 * Version: 1.0
 * Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public SysUser getUserByNP(SysUser user) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(user.getUserName());
        criteria.andUserPasswordEqualTo(user.getUserPassword());
        List<SysUser> sysUsers = userMapper.selectByExample(example);
        if (sysUsers!=null&&sysUsers.size()==1){
            return sysUsers.get(0);
        }
        return null;
    }

    @Override
    public PageInfo<SysUser> queryUserByPage(SysUser user, Integer currentPage, Integer pageSize) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        if (!StringUtils.isEmpty(user.getUserName())){
            criteria.andUserNameLike("%"+user.getUserName()+"%");
        }
        List<SysUser> sysUsers = userMapper.selectByExample(sysUserExample);
        PageHelper.startPage(currentPage,pageSize);
        return new PageInfo<>(sysUsers);
    }

    @Override
    public Boolean saveUser(SysUser user) {
        if (user.getUserId()==null||user.getUserId()==0){
//            添加
            return userMapper.insertSelective(user) == 1?true:false;
        }
        return userMapper.updateByPrimaryKeySelective(user)==1?true:false;
    }

    @Override
    public SysUser getUserById(Integer id) {
        System.out.println("用户ID："+id);
        SysUser user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public Boolean deleteById(Integer id) {
        System.out.println(id);
       return userMapper.deleteByPrimaryKey(id) == 1?true:false;
    }
}
